#!/bin/bash

# Parts taken from
# http://codeinthehole.com/tips/tips-for-using-a-git-pre-commit-hook/ and
# https://stackoverflow.com/questions/20479794/how-do-i-properly-git-stash-pop-in-pre-commit-hooks-to-get-a-clean-working-tree

# First, stash index and work dir, keeping only the staged changes in the
# working directory.
old_stash=$(git rev-parse -q --verify refs/stash)
stash_name="pre-commit-$(date +%s)"
git stash save -q --keep-index --include-untracked $stash_name
new_stash=$(git rev-parse -q --verify refs/stash)

quit() {
    # if we stashed any changes pop them
    if [ "$old_stash" != "$new_stash" ]; then
        git stash pop --index -q
    fi
    exit $1
}

# Test prospective commit
mx --suite=maxine checkstyle -f
result=$?
if [ $result -ne 0 ]; then
    echo "There are some style errors in your files, please fix them before committing"
    quit $result
fi
mx --suite=maxine checkcopyrights --modified
result=$?
if [ $result -ne 0 ]; then
    echo "There are some copyright issues in your files, please fix them before committing"
    quit $result
fi

quit 0
